package com.winit.project.core.branch;

import com.alibaba.fastjson.JSONObject;
import com.winit.jumpServer.task.PushStatusEnum;
import com.winit.project.task.BuildStatusEnum;
import com.winit.project.util.ProjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * Created by wangjian on 16/9/6.
 */
public class ProjectStatus {

    private static final Logger logger = LoggerFactory.getLogger(ProjectStatus.class);
    private static String STATUS_FILE_NAME = "status.json";

    private String path;
    private String projectName;
    private String qaTestEdition;
    private String buildStatus = BuildStatusEnum.NEVER_BUILD.getDesc();
    //跳板机推送状态
    private String pushStatus = PushStatusEnum.NEVER_PUSH.getDesc();
    private String pushDesc;
    private String pushEdition;

    //纪录最后一次点击的测试通过状态 最后一次点击过测试通过为true  最后一次点击测试不通过为false
    private boolean lastPass = true;

    public ProjectStatus(String projectName,boolean loadInfo) throws Exception{
        if(StringUtils.isEmpty(projectName)){
            logger.error("工程名不能为空");
            throw new Exception("工程名不能为空");
        }
        this.projectName = projectName;
        this.path = ProjectUtil.getProjectFilePath(projectName);
        if(loadInfo){
            this.loadFromFile();
        }
    }

    public boolean loadFromFile() throws Exception{
        JSONObject object = ProjectUtil.loadFromFile(this.path+STATUS_FILE_NAME);
        this.qaTestEdition = object.getString("qaTestEdition");
        this.buildStatus = StringUtils.isEmpty(object.getString("buildStatus"))?BuildStatusEnum.NEVER_BUILD.getDesc():object.getString("buildStatus");
        this.pushStatus = StringUtils.isEmpty(object.getString("pushStatus"))?PushStatusEnum.NEVER_PUSH.getDesc():object.getString("pushStatus");
        this.pushDesc = object.getString("pushDesc");
        this.pushEdition = object.getString("pushEdition");
        this.lastPass = StringUtils.isEmpty(object.getString("lastPass"))?true:Boolean.parseBoolean(object.getString("lastPass"));
        return true;
    }

    public boolean saveToFile() throws Exception{
        JSONObject object = new JSONObject();
        object.put("qaTestEdition",this.qaTestEdition);
        object.put("buildStatus",this.buildStatus);
        object.put("pushStatus",this.pushStatus);
        object.put("pushDesc",this.pushDesc);
        object.put("lastPass",this.lastPass);
        object.put("pushEdition",this.pushEdition);
        return ProjectUtil.saveToFile(this.path+STATUS_FILE_NAME,object);
    }

    public String getPath(){
        return this.path;
    }

    public String getQaTestEdition() {
        return qaTestEdition;
    }

    public void setQaTestEdition(String qaTestEdition) {
        this.qaTestEdition = qaTestEdition;
    }

    public String getProjectName() {
        return projectName;
    }

    public String getBuildStatus() {
        return buildStatus;
    }

    public void setBuildStatus(String buildStatus) {
        this.buildStatus = buildStatus;
    }

    public String getPushStatus() {
        return pushStatus;
    }

    public void setPushStatus(String pushStatus) {
        this.pushStatus = pushStatus;
    }

    public String getPushDesc() {
        return pushDesc;
    }

    public void setPushDesc(String pushDesc) {
        this.pushDesc = pushDesc;
    }

    public boolean isLastPass() {
        return lastPass;
    }

    public void setLastPass(boolean lastPass) {
        this.lastPass = lastPass;
    }

    public String getPushEdition() {
        return pushEdition;
    }

    public void setPushEdition(String pushEdition) {
        this.pushEdition = pushEdition;
    }
}
